<template>
    <div>
     <!-- 面包屑导航区域 -->
		<el-breadcrumb separator-class="el-icon-arrow-right">
			<el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
			<el-breadcrumb-item>商品管理</el-breadcrumb-item>
			<el-breadcrumb-item>添加商品</el-breadcrumb-item>
		</el-breadcrumb>

        <!-- 卡片视图区域 -->
		<el-card>
            <el-alert title="添加商品信息" type="info" center show-icon :closable="false"></el-alert>

            <el-steps :space="200" :active="actvieIndex - 0" finish-status="success" align-center>
                <el-step title="基本信息"></el-step>
                <el-step title="商品參數"></el-step>
                <el-step title="商品屬性"></el-step>
                <el-step title="商品圖片"></el-step>
                <el-step title="商品內容"></el-step>
                <el-step title="完成"></el-step>
            </el-steps>

            <el-form :model="addForm" :rules="addFormRules" ref="addFormRef" 
            label-width="100px" label-position="top">
                <el-tabs v-model="actvieIndex" tab-position="left" style="height: auto;"
                :before-leave="beforeTabLeave" @tab-click="tabClicked">
                    <el-tab-pane label="基本信息" name="0">
                        <el-form-item label="商品名稱" prop="goods_name">
                            <el-input v-model="addForm.goods_name"></el-input>
                        </el-form-item>
                         <el-form-item label="商品價格" prop="goods_price">
                            <el-input v-model="addForm.goods_price" type="number"></el-input>
                        </el-form-item>
                         <el-form-item label="商品重量" prop="goods_weight">
                            <el-input v-model="addForm.goods_weight"></el-input>
                        </el-form-item>
                         <el-form-item label="商品數量" prop="goods_number">
                            <el-input v-model="addForm.goods_number"></el-input>
                        </el-form-item>
                        <el-form-item label="商品分類" prop="goods_cat">
                            <el-cascader expand-trigger="hover" :options="catelist" :props="cateProps" 
                            v-model="addForm.goods_cat" @change="handleChange">
                            </el-cascader>
                        </el-form-item>
                    </el-tab-pane>
                    <el-tab-pane label="商品參數" name="1">
                         <el-form-item :label="item.attr_name" v-for="item in manyTableData" :key="item.attr_id">
                            <el-checkbox-group v-model="item.attr_vals">
                                <el-checkbox :label="cb" v-for="(cb,i) in item.attr_vals" :key="i" border></el-checkbox>
                            </el-checkbox-group>
                            
                        </el-form-item>
                    </el-tab-pane>
                    <el-tab-pane label="商品屬性" name="2">
                        <el-form-item :label="item.attr_name" v-for="item in onlyTableData" :key="item.attr_id">
                            <el-input v-model="item.attr_vals"></el-input>
                        </el-form-item>
                    </el-tab-pane>
                    <el-tab-pane label="商品圖片" name="3">
                        <el-upload :action="uploadURL" :on-preview="handlePreview" 
                        :on-remove="handleRemove" list-type="picture" :headers="headerObj" :on-success="handleSuccess">
                            <el-button type="primary" size="small">點擊上傳</el-button>
                        </el-upload>
                    </el-tab-pane>
                    <el-tab-pane label="商品內容" name="4">
                        <!-- 富文本編輯器組件 -->
                        <quill-editor v-model="addForm.goods_introduce"></quill-editor>
                        <el-button type="primary" class="btnAdd">添加商品</el-button>
                    </el-tab-pane>
                </el-tabs>
            </el-form>

        </el-card>
    </div>
</template>

<script>
export default {
    data(){
        return {
            actvieIndex:'0',
            addForm:{
                goods_name:'',
                goods_price:0,
                goods_weight:0,
                goods_number:0,
                goods_cat:[],
                //圖片的數組
                pics:[],
                goods_introduce:''
            },
            addFormRules:{
                goods_name:[{required:true,message:'請輸入商品名稱',trigger:'blur'}],
                goods_price:[{required:true,message:'請輸入商品價格',trigger:'blur'}],
                goods_weight:[{required:true,message:'請輸入商品重量',trigger:'blur'}],
                goods_number:[{required:true,message:'請輸入商品數量',trigger:'blur'}],
                goods_cat:[{required:true,message:'請選擇商品分類',trigger:'blur'}]
            },
            catelist:[],
            cateProps:{
                label:'cat_name',
                value:'cat_id',
                children:'children'
            },
            manyTableData:[],
            onlyTableData:[],
            uploadURL:"http://localhost:7721/Uploader/",
            //上傳組件的headers請求頭
            headerObj:{
                Authorization:window.sessionStorage.getItem('token')
            }
        }
    },
    created() {
        this.getCateList()
    },
    methods:{
        async getCateList(){
            const {data:res} = await this.$http.get("/vip/acategories/get",{params:this.queryInfo})
            console.log(res)
            if(res.meta.status !== 200)
            {
                return this.$message.error('获取商品分类失败')
            }
            this.catelist = res.data.cates
        },
        handleChange(){
            if(this.addForm.goods_cat.length !== 3)
            {
                this.addForm.goods_cat = []  
            }
        },
        beforeTabLeave(activeName,oldActiveName){
            if(oldActiveName === '0' && this.addForm.goods_cat.length !== 3){
                this.$message.error('請先選擇商品分類')
                return false
            }
        },
        tabClicked(){
            if(this.actvieIndex === '1'){
                //訪問動態參數面板
            }else if(this.actvieIndex == '2'){
                //靜態屬性面板
            }
        },
        handlePreview(){},
        //處理移除圖片的事件
        handleRemove(){},
        //監聽圖片上傳成功的事件
        handleSuccess(){}
    },
    computed:{
         cateId(){
            if(this.selectedCataKeys.length === 3){
                return this.selectedCataKeys[2];
            }
            return null
        }
    }
}
</script>

<style lang="less" scoped>
.el-checkbox{
    margin: 0 10px 0 0 !important;
}
</style>